package com.m7788.screen.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.m7788.screen.PaymentScreen;
import com.m7788.screen.R;
import com.m7788.util.AppUtil;
import com.m7788.util.Constant;
import com.m7788.util.HttpService;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tool.utils.DataUtil;
import com.tool.utils.StrUtils;

public class WXPayEntryActivity extends Activity implements OnClickListener,
		IWXAPIEventHandler {

	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	private IWXAPI api;

	private Button btn_left;
	private TextView tv_result;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_result);
		api = WXAPIFactory.createWXAPI(this, WXPayUtil.WEIXINAPPID);
		api.handleIntent(getIntent(), this);

		btn_left = (Button) this.findViewById(R.id.pr_btn_left);
		btn_left.setOnClickListener(this);
		tv_result = (TextView) this.findViewById(R.id.pr_tv_result);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if (resp.errCode == -1) {
				AppUtil.sendMesToActivity(WXPayEntryActivity.this, 207, "");
				if (StrUtils.isNotEmpty(resp.errStr)) {
//					DataUtil.showShortToast(WXPayEntryActivity.this,
//							("支付失败:" + resp.errStr));
					finish();
					// tv_result.setText("支付失败:"+resp.errStr);
				} else {
//					DataUtil.showShortToast(WXPayEntryActivity.this, ("支付失败"));
					finish();
					// tv_result.setText("支付失败");
				}
				
			} else if (resp.errCode == -2) {
				AppUtil.sendMesToActivity(WXPayEntryActivity.this, 207, "");
//				DataUtil.showShortToast(WXPayEntryActivity.this, ("支付已取消"));
				finish();
				// tv_result.setText("支付已取消");
			} else if (resp.errCode == 0) {
//				  DataUtil.showShortToast(WXPayEntryActivity.this, ("支付成功"));
//				  Intent intent = new Intent();
//				  intent.putExtra("operation", 205);
//				  intent.setAction(Constant.ACTION_STOA);
//				  WXPayEntryActivity.this.sendBroadcast(intent); 
//				  new UpdateQueryAsyncTask().execute();
				  
				  AppUtil.sendMesToActivity(WXPayEntryActivity.this, 204, "");
				  finish();
				// tv_result.setText("支付成功");
			}
		}
	}

	@Override
	public void onClick(View arg0) {
		finish();
	}

	private class UpdateQueryAsyncTask extends AsyncTask<Object, Object, Object> {
		@Override
		protected void onPostExecute(Object result) {
			
			if (result != null&&result.toString().length()>0) {
				
			} else {
			}
			super.onPostExecute(result);
		}

		@Override
		protected Object doInBackground(Object... params) {
			String orderid =  AppUtil.getRecord(WXPayEntryActivity.this, Constant.PAY_ORDER_ID);
			String partnerUserId = AppUtil.getRecord(WXPayEntryActivity.this, Constant.PAY_USER_ID);
			
			return HttpService.getInstance(WXPayEntryActivity.this).sendPayResult(partnerUserId, orderid);

		}
	}
	
}